package thymeleafexamples.layouts.config; import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.UrlTemplateResolver; @Configuration class WebMvcConfig extends WebMvcConfigurationSupport { private static final String MESSAGE_SOURCE = "/WEB-INF/i18n/messages"; private static final String VIEWS = "/WEB-INF/views/"; private static final String RESOURCES_LOCATION = "/resources/"; private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**"; @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping(); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); requestMappingHandlerMapping.setUseTrailingSlashMatch(false); return requestMappingHandlerMapping; } @Bean(name = "messageSource") public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename(MESSAGE_SOURCE); messageSource.setCacheSeconds(5); return messageSource; } @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setPrefix(VIEWS); resolver.setSuffix(".html"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCharacterEncoding("UTF-8"); resolver.setCacheable(false); return resolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.addTemplateResolver(new UrlTemplateResolver()); templateEngine.addTemplateResolver(templateResolver()); templateEngine.addDialect(new SpringSecurityDialect()); templateEngine.addDialect(new LayoutDialect()); templateEngine.addDialect(new Java8TimeDialect()); return templateEngine; } @Bean public ViewResolver viewResolver() { ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setTemplateEngine(templateEngine()); thymeleafViewResolver.setCharacterEncoding("UTF-8"); return thymeleafViewResolver; } @Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource()); return validator; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(RESOURCES_LOCATION); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } /** * Handles favicon.ico requests assuring no <code>404 Not Found</code> error is returned. */ @Controller static class FaviconController { @RequestMapping("favicon.ico") String favicon() { return "forward:/resources/images/favicon.ico"; } } }